<?php

namespace brand\components;

use brand\logic\BrandHelper;
use brand\logic\Config;

class Time extends Component
{

    public function _init()
    {
        $props = $this->issetFormValue('props');
        if (isset($props['isRange'])){
            //时间多选 单独处理
            $this->config['form']['props']['is-range'] = $this->config['form']['props']['isRange'];
            unset($this->config['form']['props']['isRange']);
        }


        $this->component = 'time';
    }



    public function checkFormValue()
    {

        if ($this->getFormParam('props.is-range')) {
            //区间
            $this->value = BrandHelper::string2array($this->value,Config::ARRAY_DIVISION_TIME);
        }

    }

    public function buildProps()
    {
        parent::buildProps();
        if ($this->getFormParam('props.placeholder')){
            $this->config['form']['props']['placeholder'] = '请选择'.$this->label;
        }
    }


    public function checkUpdateData($key, $value)
    {

        if ($this->getFormParam('props.is-range') ) {
            //区间
            $value = BrandHelper::array2String($value,Config::ARRAY_DIVISION_TIME);
        }
        return $value;
    }

    public function DbToCurd($field, $props=[])
    {

        $column = \brand\Brand::Builder()->form_method('time')->form_props($props)
            ->table($props)->table_action_type('time')
            ->search_condition('between');

        //时间区间
        if (isset($props['isRange']) && $props['isRange']){
            //时间区间不支持搜索
            $column->search(false);
        }

        return $column;
    }


    public function buildConfigColumn($tableConfigValue, $curd_method = 'curd_method')
    {


        $options['type'] = \brand\Brand::Builder('时间类型','select')->form_options( BrandHelper::formatOptions(['time'=>'时间','range'=>'时间区间']) )->build();
        $cloumn = \brand\Brand::Builder('时间类型','arrays')
            ->form_options([$options])
            ->form_props( [
                'append'    =>  $curd_method != 'curd_method'
            ])
            ->form_visible([['prop' => $curd_method, 'value' => 'Time']])
            ->build();


        if (isset($tableConfigValue['form']['props']['isRange']) && $tableConfigValue['form']['props']['isRange']){
            $type = 'range';
        }else{
            $type = 'time';
        }
        return ['column' => $cloumn,'value' => [['type'=>$type]]];

    }

    public function methodFormConf($form,$table,$params)
    {
        $param = $params['param'][0] ?? [];


        $props['value-format'] = 'HH:mm:ss';
        $type = $param['type'] ?? 'time';
        $props['isRange'] = $type != 'time';

        if ($props['isRange']){
            $table['action_type'] = 'time';
            $table['type'] = 'range';
        }

        $form['props'] = $props;

        return [$form,$table];
    }




}